package com.fjw.car_send;

import android.content.Context;
import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SignlView extends SurfaceView implements SurfaceHolder.Callback {

    private Paint mPaint;

    private boolean mRunning;
    private DrawingThread mDrawingThread;
    private int mWidth;


    final String LOG_TAG = "car_send_touchpad";


    public SignlView(Context context) {
        super(context);
        init();
    }

    public SignlView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SignlView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public SignlView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.GREEN);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(1);
        mPaint.setAntiAlias(true);

        SurfaceHolder surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);

        // 设置类型为LAYER_TYPE_HARDWARE以启用硬件加速
        setZOrderOnTop(false);
        surfaceHolder.setFormat(PixelFormat.TRANSPARENT);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Canvas canvas;
        canvas = holder.lockCanvas();
        holder.unlockCanvasAndPost(canvas);

        mRunning = true;
        mDrawingThread = new DrawingThread(holder);
        mDrawingThread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        mRunning = false;
        while (retry) {
            try {
                mDrawingThread.join();
                retry = false;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private class DrawingThread extends Thread {
        private SurfaceHolder mSurfaceHolder;

        public DrawingThread(SurfaceHolder surfaceHolder) {
            mSurfaceHolder = surfaceHolder;
        }

        @Override
        public void run() {
            Canvas canvas;
            while (mRunning) {
                canvas = null;
                try {
                    canvas = mSurfaceHolder.lockCanvas(null);
                    synchronized (mSurfaceHolder) {
                        doDraw(canvas);
                    }
                } finally {
                    if (canvas != null) {
                        mSurfaceHolder.unlockCanvasAndPost(canvas);
                    }
                }
            }
        }
    }

    private void doDraw(Canvas canvas) {
        // 在这里进行绘制
        if (canvas == null) {
            return;
        }
        canvas.drawColor(Color.WHITE);
        mPaint.setColor(Color.GRAY);
        if (MainActivity.getInstance().getmBluetoothGatt() == null) {
            drawDectecting(canvas);
        } else {
            drawSignal(canvas);
        }
    }


    private void drawDectecting(Canvas canvas) {

        int screenWidth = canvas.getWidth();
        int screenHeight = canvas.getHeight();
        Drawable drawable = MainActivity.getInstance().getDrawable(R.drawable.detect);

        drawable.setBounds(0, 0, screenWidth, screenHeight);
        int centerX = screenWidth / 2;
        int centerY = screenHeight / 2;
        long current = System.currentTimeMillis();
        canvas.save();
        canvas.rotate((current / 3) % 360, centerX, centerY);
        drawable.draw(canvas);
        canvas.restore();
    }

    private void drawSignal(Canvas canvas) {
        int screenWidth = canvas.getWidth();
        int screenHeight = canvas.getHeight();
        Drawable drawable;
        switch (MainActivity.getInstance().getmSignlLevel()) {
            case 0:
                drawable = MainActivity.getInstance().getDrawable(R.drawable.signal0);
                break;
            case 1:
                drawable = MainActivity.getInstance().getDrawable(R.drawable.signal1);
                break;
            case 2:
                drawable = MainActivity.getInstance().getDrawable(R.drawable.signal2);
                break;
            case 3:
                drawable = MainActivity.getInstance().getDrawable(R.drawable.signal3);
                break;
            default:
                return;

        }
        drawable.setBounds(0, 0, screenWidth, screenHeight);
        drawable.draw(canvas);

    }
}
